package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BdmComment;
import com.ruoyi.system.domain.BdmOrderItem;
import com.ruoyi.system.domain.dto.BdmCommentDto;
import com.ruoyi.system.domain.dto.BdmCommentHistoryDto;
import com.ruoyi.system.domain.vo.BdmCommentVo;
import com.ruoyi.system.mapper.BdmCommentMapper;
import com.ruoyi.system.service.IBdmCommentService;
import com.ruoyi.system.service.IBdmOrderItemService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 评论Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-23
 */
@Service
public class BdmCommentServiceImpl extends ServiceImpl<BdmCommentMapper,BdmComment> implements IBdmCommentService
{
    @Autowired
    private BdmCommentMapper bdmCommentMapper;

    @Autowired
    private IBdmOrderItemService bdmOrderItemService;

    /**
     * 查询评论
     * 
     * @param id 评论主键
     * @return 评论
     */
    @Override
    public BdmComment selectBdmCommentById(Long id)
    {
        return bdmCommentMapper.selectBdmCommentById(id);
    }

    /**
     * 查询评论列表
     * 
     * @param bdmComment 评论
     * @return 评论
     */
    @Override
    public List<BdmComment> selectBdmCommentList(BdmComment bdmComment)
    {
        return bdmCommentMapper.selectBdmCommentList(bdmComment);
    }

    /**
     * 新增评论
     * 
     * @param bdmComment 评论
     * @return 结果
     */
    @Override
    public int insertBdmComment(BdmComment bdmComment)
    {
        bdmComment.setCreateTime(DateUtils.getNowDate());
        return bdmCommentMapper.insertBdmComment(bdmComment);
    }

    /**
     * 修改评论
     * 
     * @param bdmComment 评论
     * @return 结果
     */
    @Override
    public int updateBdmComment(BdmComment bdmComment)
    {
        Date nowDate = DateUtils.getNowDate();
        bdmComment.setUpdateTime(nowDate);
        bdmComment.setMerchantReplyTime(nowDate);
        return bdmCommentMapper.updateBdmComment(bdmComment);
    }

    /**
     * 批量删除评论
     * 
     * @param ids 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteBdmCommentByIds(Long[] ids)
    {
        return bdmCommentMapper.deleteBdmCommentByIds(ids);
    }

    /**
     * 删除评论信息
     * 
     * @param id 评论主键
     * @return 结果
     */
    @Override
    public int deleteBdmCommentById(Long id)
    {
        return bdmCommentMapper.deleteBdmCommentById(id);
    }

    @Override
    @Transactional
    public boolean addComment(BdmCommentDto bdmCommentDto) {
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();

        //新增评论
        BdmComment bdmComment = BeanUtil.copyProperties(bdmCommentDto, BdmComment.class);
        bdmComment.setUid(loginUser.getUserId());//填充用户id
        bdmComment.setCreateTime(DateUtils.getNowDate());//创建时间
        bdmComment.setNickName(loginUser.getNickName());
        bdmComment.setAvatar(loginUser.getAvatar());
        //修改订单详情为已评价
        BdmOrderItem bdmOrderItem = bdmOrderItemService.lambdaQuery().eq(BdmOrderItem::getOrderId, bdmCommentDto.getOrderId()).eq(BdmOrderItem::getId, bdmCommentDto.getItemId()).one();
        if (ObjectUtils.isEmpty(bdmOrderItem)){
            throw new RuntimeException("订单不存在");
        }
        bdmOrderItem.setIfEvaluate(1L);//改为已评价
        bdmOrderItemService.updateById(bdmOrderItem);
        return this.save(bdmComment);
    }


    /**
     * 查询当前用户历史评论
     */
    @Override
    public List<BdmCommentVo> history(BdmCommentHistoryDto bdmCommentHistoryDto) {
        Long userId = SecurityUtils.getUserId();
        bdmCommentHistoryDto.setUserId(userId);
        return    bdmCommentMapper.history(bdmCommentHistoryDto);

    }

    @Override
    public List<BdmCommentVo> adminHistory(BdmCommentHistoryDto bdmCommentHistoryDto) {
        return bdmCommentMapper.history(bdmCommentHistoryDto);
    }
}
